﻿Imports System
Imports System.ComponentModel
Imports System.Reflection
Imports System.ComponentModel.Design.Serialization
Imports TnTinMn.NIB.NumericInputBox

Friend Class NIBSystemTypesEnumConverter
   Inherits TypeConverter

   Public Overloads Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
      Return True
   End Function

   Public Overloads Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
      Return New StandardValuesCollection(New NIBSystemTypes() {NIBSystemTypes.Byte, _
                                                                NIBSystemTypes.SByte, _
                                                                NIBSystemTypes.Single, _
                                                                NIBSystemTypes.Double, _
                                                                NIBSystemTypes.Decimal, _
                                                                NIBSystemTypes.Int16, _
                                                                NIBSystemTypes.Int32, _
                                                                NIBSystemTypes.Int64, _
                                                                NIBSystemTypes.UInt16, _
                                                                NIBSystemTypes.UInt32, _
                                                                NIBSystemTypes.UInt64})

   End Function

   Public Overrides Function GetStandardValuesExclusive(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
      Return True
   End Function

   Public Overrides Function ConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, _
                                         ByVal culture As System.Globalization.CultureInfo, _
                                         ByVal value As Object) As Object
      If TypeOf value Is String Then
         Select Case DirectCast(value, String)
            Case "Byte"
               Return NumericInputBox.NIBSystemTypes.Byte
            Case "SByte"
               Return NumericInputBox.NIBSystemTypes.SByte
            Case "Single"
               Return NumericInputBox.NIBSystemTypes.Single
            Case "Double"
               Return NumericInputBox.NIBSystemTypes.Double
            Case "Decimal"
               Return NumericInputBox.NIBSystemTypes.Decimal
            Case "Int16"
               Return NumericInputBox.NIBSystemTypes.Int16
            Case "Int32"
               Return NumericInputBox.NIBSystemTypes.Int32
            Case "Int64"
               Return NumericInputBox.NIBSystemTypes.Int64
            Case "UInt16"
               Return NumericInputBox.NIBSystemTypes.UInt16
            Case "UInt32"
               Return NumericInputBox.NIBSystemTypes.UInt32
            Case "UInt64"
               Return NumericInputBox.NIBSystemTypes.UInt64
            Case Else
               Throw New ArgumentException("Invalid System Type Selected")
         End Select
      End If
      Return MyBase.ConvertFrom(context, culture, value)
   End Function

   Public Overrides Function CanConvertFrom(ByVal context As System.ComponentModel.ITypeDescriptorContext, _
                                            ByVal sourceType As System.Type) As Boolean
      If sourceType Is GetType(String) Then Return True
      Return MyBase.CanConvertFrom(context, sourceType)
   End Function

   Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, _
                                       ByVal culture As System.Globalization.CultureInfo, _
                                       ByVal value As Object, _
                                       ByVal destinationType As System.Type) As Object


      If destinationType Is GetType(NumericInputBox.NIBSystemTypes) Then
         Return CType(value, NumericInputBox.NIBSystemTypes).ToString
      End If

      Return MyBase.ConvertTo(context, culture, value, destinationType)
   End Function
End Class
